package cn.yi.medicalsys.dao.impl;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.yi.medicalsys.dao.HospitalDao;
import cn.yi.medicalsys.pojo.Hospital;
import cn.yi.medicalsys.utils.BaseDao;

public class HospitalDaoImpl extends BaseDao implements HospitalDao {

	@Override
	public Hospital findHospitalByNo(Integer hid) throws Exception{
		Hospital hospital=null;
		StringBuffer preparedSql=new StringBuffer();
		preparedSql.append("SELECT hid,hosPass,cid,hospName,hospAddress,hospPhone,hospEmail,hospImage,hospIntr,hospLeader,hospState ");
		preparedSql.append(" from hospital where hid=? ");
		Object[] params={hid};
		ResultSet rs=super.executeQuery(preparedSql.toString(), params);
		while(rs.next()){
			hospital=new Hospital();
			hospital.sethId(rs.getInt("hid"));
			hospital.setHosPass(rs.getString("hosPass"));
			hospital.setCid(rs.getInt("cid"));
			hospital.setHospName(rs.getString("hospName"));
			hospital.setHospAddress(rs.getString("hospAddress"));
			hospital.setHospPhone(rs.getString("hospPhone"));
			hospital.setHospEmail(rs.getString("hospEmail"));
			hospital.setHospImage(rs.getString("hospImage"));
			hospital.setHospIntr(rs.getString("hospIntr"));
			hospital.setHospLeader(rs.getString("hospLeader"));
			hospital.setHospState(rs.getInt("hospState"));
		}
		return hospital;
	}

	@Override
	public int updateHospitalByNo(Hospital hospital) throws Exception {
		List<Object> listParams=new ArrayList<Object>();
		StringBuffer preparedSql=new StringBuffer();
		preparedSql.append("UPDATE hospital SET hid=? ");
		listParams.add(hospital.gethId());
		if(hospital.getHospPhone()!=null && !hospital.getHospPhone().equals("")){
			preparedSql.append(",hospPhone=? ");
			listParams.add(hospital.getHospPhone());
		}
		if(hospital.getHospEmail()!=null && !hospital.getHospEmail().equals("")){
			preparedSql.append(",hospEmail=? ");
			listParams.add(hospital.getHospEmail());
		}
		if(hospital.getHospIntr()!=null && !hospital.getHospIntr().equals("")){
			preparedSql.append(",hospIntr=? ");
			listParams.add(hospital.getHospIntr());
		}
		if(hospital.getHospLeader()!=null && !hospital.getHospLeader().equals("")){
			preparedSql.append(",hospLeader=? ");
			listParams.add(hospital.getHospLeader());
		}
		preparedSql.append("where hid=? ");
		listParams.add(hospital.gethId());
		Object[] params=listParams.toArray();
		int row =super.executeUpdate(preparedSql.toString(), params);
		return row;
	}

}
